package com.hazardSoft.eStore.backBeans.client;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Bill;
import com.hazardSoft.eStore.entities.Product;
import com.hazardSoft.eStore.entities.Sale;
import com.hazardSoft.eStore.entities.User;
import com.hazardSoft.eStore.integration.ProductsDelegate;
import com.hazardSoft.eStore.integration.ShoppingCarDelegate;
import com.hazardSoft.eStore.models.ClientBuyProductsModel;
import com.hazardSoft.eStore.navigation.NavigationRules;

public class ClientBuyProductsBean {

	private ClientBuyProductsModel clientBuyProductsModel;
	
	private ShoppingCarDelegate shoppingCarDelegate;
	
	public ClientBuyProductsBean() {
		
		try {
			shoppingCarDelegate = new ShoppingCarDelegate();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		clientBuyProductsModel = new ClientBuyProductsModel();
	}
	
	public String searchProducts(){
		
		ProductsDelegate productsDelegate;
		
		Product productToSearch;
		
		List<Product> productList;
		
		try {
			productsDelegate = new ProductsDelegate();
			
			productToSearch = new Product();
			productToSearch.setName(clientBuyProductsModel.getKeyWords());
			
			productList = productsDelegate.searchProducts(productToSearch);
			
			clientBuyProductsModel.setProductList(productList);
			
			if(productList.size()==0){
				FacesContext.getCurrentInstance().addMessage("noResult", new FacesMessage("No se encontraron productos"));
			}
		} catch (SystemException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String action(){
		
		ProductsDelegate productsDelegate;
		Product product;
		
		try {
			productsDelegate = new ProductsDelegate();
			
			product = new Product();
			product.setId(clientBuyProductsModel.getProductId());
			
			product = productsDelegate.searchProductbyId(product);
			
			clientBuyProductsModel.setProduct(product);
			clientBuyProductsModel.setQuantity(0);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		return NavigationRules.add_product.name();
	}
	
	public String addToBuyCar(){
		
		Sale sale;
		Product product;
		List<Sale> saleList;
		
		ProductsDelegate productsDelegate;
		
		try {
			
			product = new Product();
			product.setId(clientBuyProductsModel.getProduct().getId());
			
			productsDelegate = new ProductsDelegate();
			
			product = productsDelegate.searchProductbyId(product);
			
			sale = new Sale();
			sale.setProduct(product);
			sale.setQuantity(clientBuyProductsModel.getQuantity());
			
			shoppingCarDelegate.addProduct(sale);
			
			saleList = shoppingCarDelegate.listProducts();
			
			clientBuyProductsModel.setSaleList(saleList);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		return NavigationRules.search_product.name();
	}
	
	public void paint(OutputStream stream, Object object) throws IOException
	{
		Integer row=(Integer) object;
		
		stream.write(clientBuyProductsModel.getProductList().get(row).getImage());
    }
	
	public String removeProductFromCar(){
		
		Sale sale;
		Product product;
		
		try {
			
			product = new Product();
			product.setId(clientBuyProductsModel.getProductId());
			
			sale = new Sale();
			sale.setProduct(product);
			
			shoppingCarDelegate.removeProduct(sale);
			
			clientBuyProductsModel.setSaleList(shoppingCarDelegate.listProducts());
			
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String initBuy(){
		User user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
		clientBuyProductsModel.setUser(user);
		return NavigationRules.buy.name();
	}
	
	public String buy(){
		
		Bill bill;
		
		try {
			bill = new Bill();
			
			bill.setSaleCollection(shoppingCarDelegate.listProducts());
			
			bill.setClient(clientBuyProductsModel.getUser());
			bill.setAddress(clientBuyProductsModel.getUser().getAddress());
			
			shoppingCarDelegate.buyProducts(bill);
			
			//MENSAJE DE EXITO
			//CLEAN FIELDS
			
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public ClientBuyProductsModel getClientBuyProductsModel() {
		return clientBuyProductsModel;
	}

	public void setClientBuyProductsModel(
			ClientBuyProductsModel clientBuyProductsModel) {
		this.clientBuyProductsModel = clientBuyProductsModel;
	}
}
